//
//  MusicController.m
//  PageViewControlerDemo
//
//  Created by lgq on 2017/3/13.
//  Copyright © 2017年 liukun. All rights reserved.
//

#import "MusicController.h"

@interface MusicController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *musicTableView;

@end

@implementation MusicController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.musicTableView.delegate = self;
    self.musicTableView.dataSource = self;
    [self.musicTableView registerNib:[UINib nibWithNibName:@"CommonCell" bundle:nil] forCellReuseIdentifier:@"CommonCell"];
    self.musicTableView.tableFooterView = [UIView new];
    
    GetDataFromDouBan *getdata = [[GetDataFromDouBan alloc] init];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [getdata getData:@"https://api.douban.com/v2/music/search" type:@"subjects" keyword:@"梁静茹" completed:^(NSDictionary *data) {
        self.musicResults = [[MusicResults alloc] init:[data objectForKey:@"musics"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.musicTableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"currentPageChanged" object:@1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.musicResults.musics.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommonDataModel *movie = self.musicResults.musics[indexPath.row];
    CommonCell *cell = (CommonCell*)[tableView dequeueReusableCellWithIdentifier:@"CommonCell" forIndexPath:indexPath];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:movie.imageURL]];
    cell.titleLabel.text = movie.title;
    return cell;
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
